package com.yc.snacknet_vue.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yc.snacknet_vue.bean.CartInfo;
import com.yc.snacknet_vue.bean.MemberInfo;
import com.yc.snacknet_vue.biz.ICartInfoBiz;
import com.yc.snacknet_vue.biz.Impl.CartInfoBizImpl;
import com.yc.snacknet_vue.biz.Impl.MemberInfoBizImpl;
import com.yc.snacknet_vue.utils.RequestToObject;
import com.yc.snacknet_vue.utils.SessionKeys;

/**
 * 购物车控制层
 * @author UNO
 * @date 2021年8月10日
 * Email 2759451734@qq.com
 */
@WebServlet("/cart/*")
public class CartInfoController extends BaseServlet{

	private static final long serialVersionUID = 1L;

	//查询某个会员购物车里面的商品信息
	protected void info(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取登录信息
		Object obj = request.getSession().getAttribute(SessionKeys.CURRENTMEMLOGINACCOUNT);
		if( null == obj) { //说明未登录
			this.send(response, 501, null);  //说明未登录
			return;
		}
		MemberInfo mem = (MemberInfo)obj; //把前台登录信息存入这个对象中
		ICartInfoBiz biz = new CartInfoBizImpl();
		List<Map<String, Object>> list = biz.info(String.valueOf(mem.getMno()));
		if( null == list || list.isEmpty()) {
			this.send(response, 501, null); //说明无购物车信息
			return;
		}
		this.send(response, 200, list);
	}

	//添加购物车
	protected void finds(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取登录信息
		Object obj = request.getSession().getAttribute(SessionKeys.CURRENTMEMLOGINACCOUNT);
		if( null == obj) { //说明未登录
			this.send(response, 501, null);  //说明未登录
			return;
		}
		MemberInfo mem = (MemberInfo)obj; //把前台登录信息存入这个对象中
		ICartInfoBiz biz = new CartInfoBizImpl();
		List<CartInfo> list = biz.finds(String.valueOf(mem.getMno()));
		if( null == list || list.isEmpty()) {
			this.send(response, 501, null); //说明无购物车信息
			return;
		}
		this.send(response, 200, list);
	}

	//添加购物车
	protected void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		CartInfo cf = RequestToObject.getParamesToObject(CartInfo.class, request);
		//生成购物车编号  
		String cno = UUID.randomUUID().toString().replace("-", "");
		cf.setCno(cno);

		ICartInfoBiz biz = new CartInfoBizImpl();
		int result = biz.add(cf);
		if( result <= 0) {
			this.send(response, 500, null);
			return;
		}
		this.send(response, 200, cno);
	}
	//修改购物车中商品数量
	protected void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int num = Integer.parseInt( request.getParameter("num"));
		String cno = request.getParameter("cno");
		ICartInfoBiz biz = new CartInfoBizImpl();
		int result = biz.update(num, cno);
		if( result <= 0) {
			this.send(response, 500, null);
			return;
		}
		this.send(response, 200, null);

	}

	//删除购物车
	protected void deletes(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String cnos = request.getParameter("cno");
		ICartInfoBiz biz = new CartInfoBizImpl();
		int result = biz.deletes(cnos);
		if( result <= 0) {
			this.send(response, 500, null);
			return;
		}
		this.send(response, 200, null);
		return;
	}
	//删除购物车
	protected void findByCno(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String cnos = request.getParameter("cnos");
		ICartInfoBiz biz = new CartInfoBizImpl();
		List<CartInfo> list = biz.findByCno(cnos);
		if( null == list || list.isEmpty()) {
			this.send(response, 500, null); //说明无购物车信息
			return;
		}
		this.send(response, 200, list);  
	}


}
